package com.practise.rabbitmqdemo.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 

@Configuration
public class TopicRabbitConfig {
    //绑定键
    public final static String TOPIC_BINDING_KEY = "topic.key1";
//    public final static String TOPIC_routing_2 = "topic.queue2";

    public final static String TOPIC_EXCHANGE = "topicExchange";

    public final static String TOPIC_QUEUE_1 = "TOPIC_QUEUE_1";
    public final static String TOPIC_QUEUE_2 = "TOPIC_QUEUE_2";

    @Bean
    public Queue firstQueue() {
        return new Queue(TOPIC_QUEUE_1);
    }
 
    @Bean
    public Queue secondQueue() {
        return new Queue(TOPIC_QUEUE_2);
    }
 
    @Bean
    TopicExchange exchange() {
        return new TopicExchange(TOPIC_EXCHANGE);
    }


    /**
     * 将firstQueue和topicExchange绑定,而且绑定的键值为topic.queue1
     * 这样只要是消息携带的路由键是topic.queue1,才会分发到该队列
     * @return
     */
    @Bean
    Binding bindingExchangeMessage() {
        return BindingBuilder.bind(firstQueue()).to(exchange()).with(TOPIC_BINDING_KEY);
    }

    /**
     * 将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
     * 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
     * @return
     */
    @Bean
    Binding bindingExchangeMessage2() {
        return BindingBuilder.bind(secondQueue()).to(exchange()).with("topic.#");
    }
 
}